//
//  HomeVC.m
//  GasBusiness
//
//  Created by zfc on 2021/7/30.
//

#import "HomeVC.h"
#import "HomeNumCoCell.h"
#import "CompanyTotalVC.h"
@interface HomeVC () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIView *bottomView;

@property (nonatomic, strong) UILabel *timeLab;
@property (nonatomic, strong) UILabel *gasCarTotalLab;
@property (nonatomic, strong) UILabel *ticketTotalLab;

@property (nonatomic, strong) NSDictionary *dataDic;

@end

@implementation HomeVC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.fd_prefersNavigationBarHidden = YES;
    [self setUI];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self fetchData];
}

- (void) setUI {

    self.view.backgroundColor = ColorF5;

    [self createTopView];

    UIScrollView *scrollView = [[UIScrollView alloc] init];
//    self.scrollView.scroll
    self.scrollView.contentSize = CGSizeMake(kScreenW, kScreenH-kTopHeight-64-kBottomHeight);
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view).offset(kTopHeight+11);
        make.leading.equalTo(self.view);
        make.bottom.equalTo(self.view);
        make.width.mas_equalTo(kScreenW);
    }];
    [self createbackView];
    [self createBottomView];

}

- (void) createTopView {
    UIView *topView = [[UIView alloc] init];
    topView.backgroundColor = [UIColor bm_colorGradientChangeWithSize:CGSizeMake(kScreenW, (64+kTopHeight)) direction:IHGradientChangeDirectionVertical startColor:[[UIColor colorWithHexString:@"FF5533"] colorWithAlphaComponent:1] endColor:[UIColor colorWithHexString:@"F5F5F5"]];
    [self.view addSubview:topView];
    self.topView = topView;
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(self.view);
        make.height.mas_equalTo((64+kTopHeight));
    }];

    PButton *companyBtn = [PButton buttonWithType:UIButtonTypeCustom];
    [companyBtn setImage:[UIImage imageNamed:@"home_ic_arrow"] forState:UIControlStateNormal];
    [companyBtn setTitle:@"请选择" forState:UIControlStateNormal];
    [companyBtn setTitleColor:WhiteColor forState:UIControlStateNormal];
    companyBtn.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightBold];
    companyBtn.imagePosition = SPButtonImagePositionRight;
    [topView addSubview:companyBtn];
    [companyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topView).offset(kToolHeight+12);
        make.leading.equalTo(topView).offset(15);
    }];

    UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [nextBtn setTitle:@"" forState:UIControlStateNormal];
    [nextBtn setImage:[UIImage imageNamed:@"shouye_ic_tuichu"] forState:UIControlStateNormal];
    [topView addSubview:nextBtn];
    [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topView).offset(kToolHeight+12);
        make.trailing.equalTo(topView).offset(-15);
    }];

    WeakSelf;
    nextBtn.block = ^(UIButton *sender) {
        [weakSelf gotoCompanyTotalVC];
    };

}

- (void) fetchData {
    NSString *url = @"tiny-shop/v1/index/index";
    [[NetWorksTools shared] requestWithMethod:GET url:url params:nil successBlock:^(NSDictionary * _Nonnull response) {
        self.dataDic = response[@"data"];

        self.timeLab.text = [NSString stringWithFormat:@"更新于%@", [Untils setTimeWithTime:response[@"timestamp"] format:@"yyyy.MM.dd HH:mm"]];
        self.gasCarTotalLab.text = [NSString stringWithFormat:@"%@", self.dataDic[@"userMoneyTotal"]];
        self.ticketTotalLab.text = [NSString stringWithFormat:@"%@", self.dataDic[@"ticketTotal"]];
        [self.collectionView reloadData];
    } failureBlock:^(NSError * _Nonnull error) {

    }];
}

-(void) gotoCompanyTotalVC {
    CompanyTotalVC *vc = [[CompanyTotalVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void) createbackView {
    UIView *backView = [[UIView alloc] init];
    backView.backgroundColor = WhiteColor;
    backView.cornerRadius = 9;
    [self.scrollView addSubview:backView];
    self.backView = backView;
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.scrollView);
        make.leading.equalTo(self.scrollView).offset(8);
        make.top.equalTo(self.scrollView);
        make.height.mas_equalTo(400);
    }];

    UIView *timeView = [[UIView alloc] init];
    [backView addSubview:timeView];
    [timeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(backView);
        make.height.mas_equalTo(100);
    }];

    UILabel *lab = [[UILabel alloc] init];
    lab.text = @"今日数据";
    lab.font = [UIFont systemFontOfSize:18 weight:UIFontWeightBold];
    [timeView addSubview:lab];
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(timeView);
        make.top.equalTo(timeView).offset(32);
    }];

    UILabel *timeLab = [[UILabel alloc] init];
    timeLab.text = @"今日数据";
    timeLab.font = [UIFont systemFontOfSize:11 weight:UIFontWeightRegular];
    [timeView addSubview:timeLab];
    self.timeLab = timeLab;
    [timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(timeView);
        make.top.equalTo(lab.mas_bottom).offset(5);
    }];

    [timeView addSubview:self.collectionView];
    [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(timeView.mas_bottom);
        make.leading.trailing.bottom.equalTo(backView);
    }];
}

- (void) createBottomView {
    UIView *bottomView = [[UIView alloc] init];
    bottomView.backgroundColor = WhiteColor;
    bottomView.cornerRadius = 9;
    [self.scrollView addSubview:bottomView];
    self.bottomView = bottomView;
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.scrollView);
        make.leading.equalTo(self.scrollView).offset(8);
        make.top.equalTo(self.backView.mas_bottom).offset(10);
        make.height.mas_equalTo(100);
        make.bottom.equalTo(self.scrollView).offset(-100);
    }];

    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = ColorF5;
    [bottomView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(bottomView);
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(35);
    }];

    UIView *leftView = [[UIView alloc] init];
    [bottomView addSubview:leftView];
    [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.bottom.equalTo(bottomView);
        make.trailing.equalTo(lineView.mas_leading);
    }];

    UILabel *lab = [[UILabel alloc] init];
    lab.text = @"加气卡充值剩余总金额";
    lab.font = [UIFont systemFontOfSize:11 weight:UIFontWeightMedium];
    lab.textColor = ColorB3;
    lab.textAlignment = NSTextAlignmentCenter;
    [leftView addSubview:lab];
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(leftView).offset(5);
        make.centerX.equalTo(leftView);
        make.bottom.equalTo(leftView).offset(-27);
    }];

    UILabel *gasCarTotalLab = [[UILabel alloc] init];
    gasCarTotalLab.text = @"加气卡充值剩余总金额";
    gasCarTotalLab.font = [UIFont systemFontOfSize:18 weight:UIFontWeightBold];
    gasCarTotalLab.textColor = Color333;
    gasCarTotalLab.textAlignment = NSTextAlignmentCenter;
    [leftView addSubview:gasCarTotalLab];
    self.gasCarTotalLab = gasCarTotalLab;
    [gasCarTotalLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(leftView).offset(5);
        make.centerX.equalTo(leftView);
        make.bottom.equalTo(lab.mas_top).offset(-10);
    }];


    UIView *rightView = [[UIView alloc] init];
    [bottomView addSubview:rightView];
    [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.trailing.bottom.equalTo(bottomView);
        make.leading.equalTo(lineView.mas_trailing);
    }];

    UILabel *lab1 = [[UILabel alloc] init];
    lab1.text = @"气票剩余总金额";
    lab1.font = [UIFont systemFontOfSize:11 weight:UIFontWeightMedium];
    lab1.textColor = ColorB3;
    lab1.textAlignment = NSTextAlignmentCenter;
    [rightView addSubview:lab1];
    [lab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(rightView).offset(5);
        make.centerX.equalTo(rightView);
        make.bottom.equalTo(rightView).offset(-27);
    }];

    UILabel *ticketTotalLab = [[UILabel alloc] init];
    ticketTotalLab.text = @"气票剩余总金额";
    ticketTotalLab.font = [UIFont systemFontOfSize:18 weight:UIFontWeightBold];
    ticketTotalLab.textColor = Color333;
    ticketTotalLab.textAlignment = NSTextAlignmentCenter;
    [rightView addSubview:ticketTotalLab];
    self.ticketTotalLab = ticketTotalLab;
    [ticketTotalLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(rightView).offset(5);
        make.centerX.equalTo(rightView);
        make.bottom.equalTo(lab1.mas_top).offset(-10);
    }];
}


#pragma mark collectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 8;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HomeNumCoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeNumCoCell" forIndexPath:indexPath];
    NSDictionary *orderToday  = self.dataDic[@"orderToday"];
    if (indexPath.item == 0) { //今日订单数量

        cell.descLab.text = @"今日订单数量";
        cell.countLab.text = [NSString stringWithFormat:@"%@", orderToday[@"count"]];
    }

    if (indexPath.item == 1) { //今日订单完成量
        cell.descLab.text = @"今日订单完成量";
        cell.countLab.text = [NSString stringWithFormat:@"%@", self.dataDic[@"orderTodayAccomplish"]];
    }

    if (indexPath.item == 2) { //今日加气卡充值金额
        cell.descLab.text = @"今日加气卡充值金额";
        cell.countLab.text = [NSString stringWithFormat:@"%@", self.dataDic[@"userMoneyTodayChange"]];
    }

    if (indexPath.item == 3) { //今日气票充值今个
        cell.descLab.text = @"今日气票充值金额";
        cell.countLab.text = [NSString stringWithFormat:@"%@", self.dataDic[@"ticketTodayChange"]];
    }

    if (indexPath.item == 4) { //今日微信支付金额
        cell.descLab.text = @"今日微信支付金额";
        cell.countLab.text = [NSString stringWithFormat:@"%@", self.dataDic[@"wechatTodayPayMoney"]];
    }

    if (indexPath.item == 5) { //今日支付宝支付金额
        cell.descLab.text = @"今日支付宝支付金额";
        cell.countLab.text = [NSString stringWithFormat:@"%@", self.dataDic[@"aliTodayPayMoney"]];
    }

    if (indexPath.item == 6) { //今日加气卡支付金额
        cell.descLab.text = @"今日加气卡支付金额";
        cell.countLab.text = [NSString stringWithFormat:@"%@", self.dataDic[@"userMoneyTodayPayMoney"]];
    }

    if (indexPath.item == 7) { //今日气票支付金额
        cell.descLab.text = @"今日气票支付金额";
        cell.countLab.text = [NSString stringWithFormat:@"%@", self.dataDic[@"ticketTodayPayMoney"]];
    }


    return cell;
}


#pragma mark - lazy
- (UICollectionView *) collectionView{
    if (!_collectionView) {
        NSInteger itemW = (kScreenW-16)/3;
        UICollectionViewFlowLayout *layout =  [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        layout.headerReferenceSize = CGSizeMake(kScreenW, 0);
        layout.footerReferenceSize = CGSizeMake(kScreenW, 0);

        layout.itemSize = CGSizeMake(itemW, 100);
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);

        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.backgroundColor = WhiteColor;
        [_collectionView registerClass:[HomeNumCoCell class] forCellWithReuseIdentifier:@"HomeNumCoCell"];

    }
    return _collectionView;
}

@end
